package Server.Socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import Server.DAO.UserDAO;
import Server.Service.DealService;

public class ClientThread extends Thread {
  
	Socket clientSocket;
	
	DataInputStream in = null;
	DataOutputStream out = null;
	
	ServerThread serverThread;
	String str;
	public static int ConnectNumber = 0;
	
	DealService service = new DealService();
	
	public ClientThread(Socket socket,ServerThread serverThread){
		clientSocket = socket;
		this.serverThread = serverThread;
		try
		{
			in = new DataInputStream(clientSocket.getInputStream());
			out = new DataOutputStream(clientSocket.getOutputStream());
		}catch(IOException e){
			System.out.print("发生异常，建立失败"+e.toString());
			System.exit(3);
		}
	}
	
	public void run(){
		while(true){
			try
			{
				String Message = in.readUTF();
				Server.jTextArea1.append(Message+"\n");
				if((Message.substring(0,5)).equals("ByeBye")){
					in.close();
					out.close();
					clientSocket.close();
					break;
				}
				out.writeUTF(calculate(Message));
			}catch(IOException e2){
				break;
			}
		}
	}
	
	public String calculate(String message){
		String tempString = "Server";
		double accountMoney = service.deal(message);
		tempString += accountMoney;
		System.out.println(accountMoney);
		return tempString;
	}
}
